import NormalStuff from "./normal.class";

class TicketStuff extends NormalStuff {
  private offsetBackStagePass = new Map<
    (p: number) => boolean,
    (o: OUTPUT) => void
  >();

  private readonly LAST_TEN_DAYS_TICKET = 10;
  private readonly LAST_FIVE_DAYS_TICKET = 5;

  constructor(sellIn: number, quality: number) {
    super(sellIn, quality);

    this.initOffsetMapper();
  }

  private initOffsetMapper() {
    this.offsetBackStagePass.set(
      function (sellIn: number) {
        return (
          sellIn > this.LAST_FIVE_DAYS_TICKET &&
          sellIn <= this.LAST_TEN_DAYS_TICKET
        );
      }.bind(this),
      function () {
        this.quality += this.OFFSET_QUALITY_TWO;
      }.bind(this)
    );
    this.offsetBackStagePass.set(
      function (sellIn: number) {
        return (
          sellIn <= this.LAST_FIVE_DAYS_TICKET &&
          sellIn >= this.EXPIRATION_SELL_IN
        );
      }.bind(this),
      function () {
        this.quality += this.OFFSET_QUALITY_THREE;
      }.bind(this)
    );
    this.offsetBackStagePass.set(
      function (sellIn: number) {
        return sellIn > this.LAST_TEN_DAYS_TICKET;
      }.bind(this),
      function () {
        this.quality -= this.OFFSET_QUALITY_ONE;
      }.bind(this)
    );
    this.offsetBackStagePass.set(
      this.isExpiration.bind(this),
      function () {
        this.quality = this.MIN_QUALITY;
      }.bind(this)
    );
  }

  updateQuality(): number {
    for (let [fn, offsetFn] of this.offsetBackStagePass) {
      if (fn(this.sellIn)) {
        offsetFn();
        break;
      }
    }
    return (this.quality = this.cleanQuality(this.quality));
  }
}

export default TicketStuff;
